package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.Movie;
import com.woniuxy.domain.Product;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.dto.ProductDto;
import com.woniuxy.entity.ProductElasticsearchDemo;
import com.woniuxy.param.ProductParam;
import com.woniuxy.service.ProductService;
import com.woniuxy.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private RedisConfig redisConfig;
    @Resource
    private ProductClientMovie productClientMovie;
    @Autowired
    private ElasticsearchRestTemplate template;
    @GetMapping("/selectProductByRid")
    public List<Product> selectProductByRid(int rid){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("r_id",rid);
        List<Product> list = productService.list(wrapper);
        return list;
    }
    //实现排片功能
    @PostMapping("/insertProduct")
    public JsonResultDto insertProduct(ProductParam productParam){
//        Product product = new Product();
//        BeanUtils.copyProperties(productParam,product);
//        productService.save(product);
        ProductElasticsearchDemo productElasticsearchDemo = new ProductElasticsearchDemo(
                productParam.getPStarttime(),
                productParam.getRName(),
                productParam.getMName(),
                productParam.getMTimelength(),
                "1",
                productParam.getPPrice()
        );
        template.save(productElasticsearchDemo);
        //传入的m_id到MovieService找到movie的所有信息
        if(!redisConfig.opsForHash().hasKey("MovieTimes", productParam.getMId())) {
            Movie movie = productClientMovie.selectMovieByMid(productParam.getMId());
            redisConfig.opsForHash().put("MovieTimes", productParam.getMId(),movie);
            return new JsonResultDto("1005", "新增场次成功", null, null);
        }else {
            System.out.println(redisConfig.opsForHash().get("MovieTimes", productParam.getMId()));
            Movie movie = (Movie) (redisConfig.opsForHash().get("MovieTimes", productParam.getMId()));
            int nowNum = Integer.parseInt(movie.getmWathchPerson()) + 1;
            movie.setmWathchPerson(String.valueOf(nowNum));
            redisConfig.opsForHash().put("MovieTimes", productParam.getMId(), movie);
            return new JsonResultDto("1005", "新增场次成功", null, null);
        }
    }
    //只显示状态为1,未过时间点,已上架的电影
    //展示根据product表p_status为1未开始,已上映影片过了时间点,会自动改变状态为0
    @GetMapping("/showValidMovieList")
    public JsonResultDto showValidMovieList(){
        List<Product> list = productService.list(new QueryWrapper<Product>().eq("p_status", "1"));
        List<ProductDto> productDtos = BeanCopyUtil.copyListPropertie(list, ProductDto::new);
        return new JsonResultDto("1000","查询可订购影片",null,productDtos);
    }
    //定时器决定电影状态m_status
//    @Scheduled(fixedDelay = 300000)
//    public void changeProductStatusScheduling(){
//        List<ProductDto> list = showValidMovieList().getList();
//        for(ProductDto productDto:list){
//            //如果开映时间毫秒数小于现在时间无法更改状态为0前端不显示影片
//            if((productDto.getPStarttime().getTime())<(new Date().getTime())){
//                Product product = new Product();
//                BeanUtils.copyProperties(productDto,product);
//                product.setpStatus("0");
//                productService.updateById(product);
//            }
//        }
//    }
    @Transactional
    @GetMapping("/productTest")
    public JsonResultDto productTest(){
        Product product = new Product();
        product.setmName("ceshiceshi");
        productService.save(product);
        int i = 1/0;
        return new JsonResultDto("400",null,null,null);
    }
//
}

